﻿using AutoMapper;
using B.S.DataBasic.Domain.GoodInfra;
using B.S.DataBasic.ErrorCode;
using B.S.DataBasic.Infrastructure.Implement;
using B.S.DataBasic.Write.API.Dtos;
using MediatR;

namespace B.S.DataBasic.Write.API.Application
{
    public class DelTranceCommandHandler : IRequestHandler<DelTranceCommand, APIResult<Traceabilitable>>
    {
        private readonly IBaseRepository<Traceabilitable> TraceRepository;
        private readonly IMapper mapper;
        public DelTranceCommandHandler(IBaseRepository<Traceabilitable> TraceRepository, IMapper mapper)
        {
            this.TraceRepository = TraceRepository;
            this.mapper = mapper;
        }
        public async Task<APIResult<Traceabilitable>> Handle(DelTranceCommand request, CancellationToken cancellationToken)
        {
            APIResult<Traceabilitable> result = new APIResult<Traceabilitable>();
            result.Code = EnumCode.删除成功;
            result.Message = EnumCode.删除成功.ToString();

            var list = mapper.Map<Traceabilitable>(request);
            list.IsDeleted = true;
            TraceRepository.UpdateAsync(list);

            return await Task.FromResult(result);
        }
    }
}
